﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;


namespace BookApp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            for (int i = 0; i < 2; i++)
            {
                Console.WriteLine("Hello World!");
            }
            
            var fieids = typeof(MicroIce.HTML.Values).GetFields();
            Console.WriteLine(MicroIce.HTML.Values.PropertiesMatch);
            Console.WriteLine(MicroIce.HTML.Values.NoneHtmlMatch);

            BookNet bookNet = new BookNet();
            string title = bookNet.GetBookNameById(2229);

            Console.WriteLine(title);

            var xxx = bookNet.GetChapters("2/2229");
            Console.WriteLine();

            //http://acgzj.cn/uwp/index.uwml
            bookNet.testuwp();
            
        }
    }

    public class BookNet
    {
        private readonly HttpClient httpClient;

        // IEnumerable<string> BooKNames;

        public BookNet()
        {
            httpClient = new HttpClient();
        }

        public void testuwp()
        {
            byte[] webbytes = httpClient.GetByteArrayAsync($"http://acgzj.cn/uwp/index.uwml").Result;
            string webtext = Encoding.UTF8.GetString(webbytes);
            MicroIce.HTML.HDoc newDocument = MicroIce.HTML.HtmlConvert.DeserializeHtml(webtext);

            Console.WriteLine();
        }

        /// <summary>
        /// 获取 列表
        /// </summary>
        //public void GetBookList()
        //{

        //}
        //https://www.wenku8.net/novel/2/2229/index.htm
        public string GetChapters(string text)// 2/2229
        {
            byte[] webbytes = httpClient.GetByteArrayAsync($"https://www.wenku8.net/novel/{text}/index.htm").Result;
            webbytes = Encoding.Convert(Encoding.GetEncoding("GBK"), Encoding.UTF8, webbytes);
            string webtext = Encoding.UTF8.GetString(webbytes);
            MicroIce.HTML.HDoc newDocument = MicroIce.HTML.HtmlConvert.DeserializeHtml(webtext);
            ///html/body/table/tbody/tr[1]/td
            MicroIce.HTML.HTag table_element = newDocument["html"]["body"]["table"];

            List<MicroIce.HTML.HTag> tds = new List<MicroIce.HTML.HTag>();
            foreach (var Tr in table_element.Children)
            {
                foreach (var td in Tr)
                {
                    tds.Add(td);
                }
            }

            return "";
        }

        public string GetBookNameById(int bookId)
        {
            //httpClient.GetStringAsync() 只有 uft8 可以，其他都会有乱码bug啊、
            //其他编码GetByteArrayAsync 再Encoding 转码
            byte[] webbytes = httpClient.GetByteArrayAsync($"http://www.wenku8.net/book/{bookId}.htm").Result;
            File.WriteAllBytes("gbk.txt", webbytes);
            webbytes = Encoding.Convert(Encoding.GetEncoding("gbk"), Encoding.UTF8, webbytes);
            File.WriteAllBytes("utf8.txt", webbytes);
            string webtext = Encoding.UTF8.GetString(webbytes);
            int a = webtext.IndexOf("<title>");
            int b = webtext.IndexOf("</title>");
            string res = webtext.Substring(a, b - a + 8);
            File.WriteAllText("res.txt", res);
            return res;
        }

    }

}
